use axum::body::{to_bytes, Body};
use axum::http::{Request, StatusCode};
use template::router::get_router;
use template::vo::user::{UserLoginReqVo, UserReqVo};
use template::AppConfig;
use tower::ServiceExt;

#[tokio::test]
async fn test_health_endpoint() {
    // 加载配置
    let config = AppConfig::load().expect("Failed to load config");

    // 创建应用
    let app = get_router(config).await.unwrap();

    // 创建健康检查请求
    let request = Request::builder()
        .method("POST")
        .header("Authorization", "Bearer eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3NTQwNjAzODksImV4cCI6MTc1NDY2NTE4OSwibmJmIjoxNzU0MDYwMzg5LCJpc3MiOiJjaGF0X3NlcnZlciIsImF1ZCI6ImNoYXRfY2xpZW50IiwiaWQiOjUsIm5hbWUiOiIxMjMzMzMiLCJwaG9uZSI6IjEzMjgzMzM5NjE2IiwiZW1haWwiOiIxMjMzMzMzQHFxLmNvbSIsImFnZSI6MSwiZGVzY3JpcHRpb24iOiIxMzMzMzMzMzIzIiwic2V4IjoxLCJzdGF0dXMiOjEsImNyZWF0ZV9hdCI6IjE5NzAtMDEtMDEgMDg6MDA6MDAiLCJ1cGRhdGVfYXQiOiIxOTcwLTAxLTAxIDA4OjAwOjAwIn0.C8l5EsSNRr0Ehr26hISxGjgqlhB2-k5cETXObz1deDGJbPqnWjLTQX8ZAagCzhNcnwDq4dKRVreZONQA_94xCQ")
        .uri("/api/signup")
        .body(Body::empty())
        .unwrap();

    // 发送请求并获取响应
    let response = app.oneshot(request).await.unwrap();

    // 验证响应体
    let body = response.into_body();
    let bytes = to_bytes(body, usize::MAX).await.unwrap();
    let body_str = str::from_utf8(&bytes).unwrap().to_string();
    println!("{body_str:?}");
}

#[tokio::test]
async fn test_chat_signin_endpoint() {
    // 加载配置
    let config = AppConfig::load().expect("Failed to load config");

    // 创建应用
    let app = get_router(config).await.unwrap();

    let u = UserReqVo {
        name: "123333".to_string(),
        phone: "13283339616".to_string(),
        age: 1,
        sex: 1,
        email: "1233333@qq.com".to_string(),
        password: "1233333333".to_string(),
        description: "1333333323".to_string(),
    };
    // 创建登录请求
    let request = Request::builder()
        .method("POST")
        .uri("/api/register")
        .body(Body::new(serde_json::to_string(&u).unwrap()))
        .unwrap();

    // 发送请求并获取响应
    let response = app.oneshot(request).await.unwrap();

    // // 验证状态码
    // assert_eq!(response.status(), StatusCode::OK);

    // 验证响应体
    let body = response.into_body();
    let bytes = to_bytes(body, usize::MAX).await.unwrap();
    let body_str = str::from_utf8(&bytes).unwrap().to_string();
    println!("{body_str:?}");
}

#[tokio::test]
async fn test_chat_signup_endpoint() {
    // 加载配置
    let config = AppConfig::load().expect("Failed to load config");

    let u = UserLoginReqVo {
        email: "1233333@qq.com".to_string(),
        password: "1233333333".to_string(),
    };
    // 创建应用
    let app = get_router(config).await.unwrap();

    // 创建注册请求
    let request = Request::builder()
        .method("POST")
        .uri("/api/sign")
        .body(Body::new(serde_json::to_string(&u).unwrap()))
        .unwrap();

    // 发送请求并获取响应
    let response = app.oneshot(request).await.unwrap();

    // 验证状态码
    assert_eq!(response.status(), StatusCode::OK);

    // 验证响应体
    let body = response.into_body();
    let bytes = to_bytes(body, usize::MAX).await.unwrap();
    let body_str = str::from_utf8(&bytes).unwrap().to_string();
    println!("{body_str:?}");
}

// 测试不存在的路由
#[tokio::test]
async fn test_not_found_route() {
    // 加载配置
    let config = AppConfig::load().expect("Failed to load config");

    // 创建应用
    let app = get_router(config).await.unwrap();

    // 创建请求到不存在的路由
    let request = Request::builder()
        .uri("/non-existent-route")
        .body(Body::empty())
        .unwrap();

    // 发送请求并获取响应
    let response = app.oneshot(request).await.unwrap();

    let body = response.into_body();
    let bytes = to_bytes(body, usize::MAX).await.unwrap();
    let body_str = str::from_utf8(&bytes).unwrap().to_string();
    println!("{body_str:?}");
}
